package com.syy.quartz.job;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class CronJobExample {
    public static void main(String[] args) {
        try {
            // 1. 创建Scheduler
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            // 2. 定义作业并绑定作业类
            JobDetail job = JobBuilder.newJob(Job.class)
                    .withIdentity("helloJob", "group1")
                    .build();

            // 3. 创建CronTrigger触发器
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("myTrigger", "group1")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) // 每5秒执行一次
                    .build();

            // 4. 注册作业和触发器
            scheduler.scheduleJob(job, trigger);

            // 5. 启动Scheduler
            scheduler.start();

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
    }
}
